package com.yanqu.road.server.manager.activity.apprentice.handler.impl;

import com.yanqu.road.entity.activity.apprentice.ApprenticeEffectBuffProject;
import com.yanqu.road.entity.activity.apprentice.config.ApprenticeProjectConfig;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeBuffProjectData;
import com.yanqu.road.entity.enums.eOccupationType;
import com.yanqu.road.server.manager.activity.apprentice.handler.ApprenticeProjectHandler;
import com.yanqu.road.server.manager.activity.apprentice.handler.param.*;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class ApprenticeDoProjectType12Handler implements ApprenticeProjectHandler {

    /**
     * 执行项目
     * @param req
     * @return
     */
    @Override
    public ApprenticeDoProjectResp doProject(ApprenticeDoProjectReq req) {
        return null;
    }

    /**
     * |接下来{0}次行程，|{1}概率天资{2}，{3}概率天资{4}。
     * @param req
     */
    @Override
    public void countTalentTypeAddRate(ApprenticeCountTalentAddRateReq req) {
        ApprenticeProjectConfig projectConfig = req.getProjectConfig();
        Map<Integer, Integer> resultMap = req.getResultMap();
        ApprenticeBuffProjectData buffProjectData = req.getBuffProjectData();
        //buff次数减一
        buffProjectData.addNum(-1);
        //计算
        List<String> paramList = StringUtils.stringToStringList(projectConfig.getParam(), "\\|");
        Map<Integer, Integer> wMap = new HashMap<>();
        List<Integer> addRateList = new ArrayList<>();
        int index = 0;
        for (String param : paramList) {
            String[] paramArr = param.split(",");
            wMap.put(index, Integer.parseInt(paramArr[0]));
            addRateList.add(Integer.parseInt(paramArr[1]) - 1000);
            index++;
        }
        int resultIndex = RandomHelper.getRandomKeyByWeight(wMap);
        int addRate = addRateList.get(resultIndex);
        //注入加成值到MAP
        for (eOccupationType type : eOccupationType.values()) {
            int talentType = type.getValue();
            int oldRate = resultMap.getOrDefault(talentType, 0);
            resultMap.put(talentType, oldRate + addRate);
        }
        //生效buff结果结果集注入
        List<ApprenticeEffectBuffProject> effectBuffProjects = req.getEffectBuffProjects();
        ApprenticeEffectBuffProject effectBuffProject = new ApprenticeEffectBuffProject(buffProjectData.getProjectId(), addRate);
        effectBuffProjects.add(effectBuffProject);
    }

    @Override
    public void countSortTypeAddRate(ApprenticeCountSortAddRateReq req) {

    }
}
